package main

import (
	"context"
	"fmt"
	"os/exec"
	"time"
)

type result struct {
	msg []byte
	err error
}

func main() {
	var (
		ctx context.Context
		ctxfun context.CancelFunc
		cmd *exec.Cmd
		resChan chan *result
		res *result
	)
	resChan = make(chan *result)
	ctx,ctxfun = context.WithCancel(context.TODO())
	go func() {
		var (
			output []byte
			err error
		)
		cmd = exec.CommandContext(ctx,"/bin/bash","-c","sleep 2;echo hello;")
		output,err = cmd.CombinedOutput()
		resChan <- &result{
			msg:output,
			err:err,
		}
	}()

	time.Sleep(1 * time.Second)
	ctxfun()
	res = <- resChan
	if res.err != nil{
		fmt.Println("err is",res.err)
	}
	fmt.Println("res",string(res.msg))
}
